#include"Comm.hpp"
class Init
{
    public:
    Init()
    {
        int n=mkfifo(pipeFile.c_str(),mode);
        if(n<0)
        {
            std::cerr<<"mkfifo error:"<<strerror(errno)<<std::endl;
        }
        else
        {
        std::cout<<"success"<<std::endl;
        }
    }
    ~Init()
    {
       
    }
};
Init init;
class Server{
    int _fd=defaultfd;
    public:
    Server()
    {

    }
    bool OpenPipe()
    {//管道如果读端打开，写端未打开，会在open这阻塞
        OPEN(openForRead,_fd);
        return true;
        
    }
    int RecvPipe(std::string*str)
    {
        char message[gsize]={0};
        ssize_t num=::read(_fd,message,sizeof(message)-1);
        if(num==0)
        {
            //命名管道已经关闭
            ClosePipe();
            std::cout<<"client quit,me too"<<std::endl;
            exit(0);
        }
        else if(num<0)
        {
            std::cerr<<"openfifo error:"<<strerror(errno)<<std::endl;
        }
        else{
            message[num]=0;
            *str=message;
        }
        return num;
    }
    void ClosePipe()
    {
        CLOSEPIPE(_fd);
    }
    ~Server()
    {
        ClosePipe();
    }
};